// GeoControl.cpp: implementation of the CGeoControl class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "GeoControl.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

#define max(a,b)    (((a) > (b)) ? (a) : (b))

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////
// CIconCombo

const int nIconMargin = 3;

CIconCombo::CIconCombo (CBCGPToolBarImages& imageListIcons) :
m_imageListIcons (imageListIcons)
{
}
//*****************************************************************************************
CIconCombo::~CIconCombo()
{
}

BEGIN_MESSAGE_MAP(CIconCombo, CBCGPComboBox)
//{{AFX_MSG_MAP(CIconCombo)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CIconCombo message handlers

void CIconCombo::DrawItem(LPDRAWITEMSTRUCT lpDIS) 
{
	CDC* pDC = CDC::FromHandle (lpDIS->hDC);
	ASSERT_VALID (pDC);
	
	CRect rect = lpDIS->rcItem;
	int nIcon = lpDIS->itemID;
	
	HBRUSH		brBackground;
	COLORREF	clText; 
	
	if (lpDIS->itemState & ODS_SELECTED)  
	{
		brBackground = GetSysColorBrush (COLOR_HIGHLIGHT); 
		clText = globalData.clrTextHilite; 
	} 
	else 
	{
		brBackground = GetSysColorBrush (COLOR_WINDOW); 
		clText = globalData.clrWindowText;
	} 
	
	if (lpDIS->itemAction & (ODA_DRAWENTIRE | ODA_SELECT))
	{
		::FillRect (lpDIS->hDC, &rect, brBackground);
	}
	
	if (nIcon < 0)
	{
		return;
	}
	
	CBCGPDrawState ds;
	m_imageListIcons.PrepareDrawImage (ds);
	m_imageListIcons.Draw (pDC, 
		rect.left + nIconMargin,
		rect.top + nIconMargin / 2,
		nIcon);
	m_imageListIcons.EndDrawImage (ds);
	
   AFX_MANAGE_STATE(AfxGetStaticModuleState());
   CString str;
	str.Format (IDS_STRING_SIGN, nIcon + 1);
	
	CFont* pOldFont = pDC->SelectObject (&globalData.fontRegular);
	
	pDC->SetBkMode (TRANSPARENT);
	pDC->SetTextColor (clText);
	
	CRect rectText = rect;
	rectText.left += m_imageListIcons.GetImageSize ().cx + 2 * nIconMargin;
	
	pDC->DrawText (str, rectText, DT_SINGLELINE | DT_VCENTER);
	
	pDC->SelectObject (pOldFont);
}
//*****************************************************************************************
void CIconCombo::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct) 
{
	CClientDC dc (this);
	CFont* pOldFont = dc.SelectObject (&globalData.fontRegular);
	
	CString str = _T("Image 99");
	int nTextWidth = dc.GetTextExtent (str).cx + nIconMargin;
	
	dc.SelectObject (pOldFont);
	
	lpMeasureItemStruct->itemWidth = m_imageListIcons.GetImageSize ().cx + nTextWidth + 2 * nIconMargin;
	lpMeasureItemStruct->itemHeight = 
		2 * nIconMargin + max (globalData.GetTextHeight (), m_imageListIcons.GetImageSize ().cy);
}